package com.example.template.controller;

import com.example.template.common.api.ApiResult;
import com.example.template.common.config.SiteOptions;
import com.example.template.common.storage.StorageFactory;
import com.example.template.common.storage.minio.MinioUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * @className: TestController
 * @description: TODO 类描述
 * @author: Kuangkeli
 * @date: 2024/01/22 16:16
 **/
@RestController
public class TestController {

    @Autowired
    private MinioUtil minioUtil;

    @Autowired
    protected StorageFactory storageFactory;
    @Autowired
    protected SiteOptions siteOptions;

    @PostMapping("/fileUpload")
    public @ResponseBody ApiResult fileUpload(@RequestParam("file") MultipartFile file) {
        final String s = minioUtil.putObject(file, file.getOriginalFilename());
        return ApiResult.success("成功");
    }

    @PostMapping("/fileDelete/{url}")
    public  ApiResult fileDelete(@PathVariable("url") String url) {
        storageFactory.get().deleteFile(url);
        return ApiResult.success("成功");
    }

    @ResponseBody
    @GetMapping("/fileDownload/{filename}")
    public void download(@PathVariable("filename") String filename, HttpServletResponse response) {
        minioUtil.getObject(filename, response);
    }

}
